<?php 
	class p extends Model {

    
    function p()
    {
       
        parent::Model();
        
    }
	
	function get_thumbail($file)
	{
			if(!($file["thumbnail"]=="")){
			return '<img src="'.base_url().'/timthumb.php?src=uploads/'.$file['thumbnail'].'&w=200&h=150&zc=1" alt="" />';
			}
		
	}
	function request_list($rows)
	{
		$data["rows"] = $rows;
		return $this->load->view("admin/request_list_views",$data,true);
		
		
	}
	function maintain_list($rows)
	{
		$data["rows"] = $rows;
		return $this->load->view("admin/maintain_list",$data,true);
	}
	function maintain_history($id)
	{
		$header =
		 array( 
			 
			  array("class"=>"left","name"=>"year","label"=>"วันที่แจ้งซ่อม"),
			  array("class"=>"","name"=>"budget_id","label"=>"ชนิดงานซ่อม"),
			   array("class"=>"","name"=>"budget_id","label"=>"อาการชำรุด"),
			  array("class"=>"","name"=>"budget_id","label"=>"บริษัทผู้ซ่อมบำรุง"),
			  array("class"=>"","name"=>"budget_id","label"=>"ราคาซ่อม"),
			  array("class"=>"","name"=>"budget_id","label"=>"รับประกันงานซ่อม")
	 			);
		$rows = $this->db->query("select * from budgets")->result_array();
		$data["header"] = $header; 
		$data["rows"] = array();
		
		return $this->load->view("admin/history_list",$data,true);
	}
    function material_view($id)
    {
    	$sql ="SELECT
				materials.material_id,
				materials.`name`,
				materials.model,
				materials.`code`,
				materials.category_id,
				materials.type_id,
				materials.buy_date,
				materials.buy_price,
				materials.brand,
				materials.warranty,
				materials.detail,
				materials.responsible,
				materials.thumbnail,
				materials.image2,
				materials.create_date,
				materials.last_modify,
				materials.`year`,
				materials.budget_id,
				materials.place_id,
				materials.company_id,
				company.company_name,
				company.address,
				company.tel,
				company.fax,
				place.place_name,
				material_categories.category_name,
				budgets.budget_name,
				budgets.`year`
				FROM
				materials
				left JOIN company ON materials.company_id = company.company_id
				left JOIN place ON materials.place_id = place.place_id
				left JOIN material_categories ON materials.category_id = material_categories.category_id
				left JOIN budgets ON materials.budget_id = budgets.budget_id
				 where material_id = $id";
    	
    	
    	$rows = $this->db->query($sql)->result_array();
    	
    	
    	
    	$data["rows"] = array(
    	 
    	array( "field"=>"ชื่อ:","detail"=>$rows[0]['name'] ),
    	array( "field"=>"เลขที่ครุภัณฑ์:","detail"=>$rows[0]["code"] ),
    	array( "field"=>"หมวด:","detail"=>$rows[0]["category_name"] ),
    	array( "field"=>"ยี้ห้อ:","detail"=>$rows[0]["brand"] ),
    	array( "field"=>"รุ่น:","detail"=>$rows[0]["model"] ),
    	array( "field"=>"วันที่ซื้อ:","detail"=>$rows[0]["buy_price"] ),
    	array( "field"=>"ราคาซื้อมา:","detail"=>$rows[0]["buy_date"] ),
    	array( "field"=>"การรับประกัน:","detail"=>$rows[0]["warranty"] ),
    	array( "field"=>"ซื่อจากบริษัท:","detail"=>$rows[0]["company_name"]." ".$rows[0]["address"] ." ".$rows[0]["tel"] ),
		array( "field"=>"รูป:","detail"=>$this->get_thumbail($rows[0]) ),
		array( "field"=>"แจ้งซ่อม:","detail"=>"" ),
		array( "field"=>"ประวัติงานซ่อม:","detail"=>$this->maintain_history($id["material_id"]) )
    	
    	
    	);
    	
    	
    	
    	$header =
		 array( 
			  array("class"=>"left width200 tdlabel","name"=>"field","label"=>"Field"),
			  array("class"=>"left","name"=>"detail","label"=>"รายละเอียด"),
			 
	 			);
		
		
	 	$data["header"] = $header; 
		
		
    	
    	
    	
    	return $this->load->view("admin/material_view",$data,true);
    	
    	
    	//$this->db->query()->result_array();
    	
	}
	
	function category_options($in_stork=0,$type_id =0)
	{
		$data["in_stork"] = $in_stork;
		$data["type_id"] = $type_id;
		$this->load->view("category_options",$data);
	}
	function barcode($prefix='M2',$view = 1)
	{
		
		
		
		$refcode = $this->db->query("select * from ref_code where ref_code = '$prefix'")->result_array();
		
		
		if($refcode)
		{	
			$this->db->query("update ref_code set ref_no = ref_no+1 where ref_code='$prefix'");
			return date("y").str_pad($refcode[0]['ref_no'],5,0,STR_PAD_LEFT);
		}
	
	}
	function barcode_auto()
	{
		$refcode = $this->db->query("select * from ref_code where ref_code = 'BARCODE'")->result_array();
		
		
		
			$this->db->query("update ref_code set ref_no = ref_no+1 where ref_code='BARCODE'");
			return date("y").date('m').str_pad($refcode[0]['ref_no'],8,0,STR_PAD_LEFT);
	}
	function date_filter_form($action="",$date_start,$date_stop)
	{
	    $data["action"] = $action;
	    $data["date_start"] = $date_start;
	    $data["date_stop"] = $date_stop;
		return $this->load->view("date_filter_form",$data,true);
	
	}
	function report_table($head_field,$rows)
	{
		$data = array();
		$data["head_field"] = $head_field;
		$data["rows"] =  $rows;
		return $this->load->view("report_table",$data,true);
		
	}
	function num2thai($number){
		$t1 = array("ศูนย์", "หนึ่ง", "สอง", "สาม", "สี่", "ห้า", "หก", "เจ็ด", "แปด", "เก้า");
		$t2 = array("เอ็ด", "ยี่", "สิบ", "ร้อย", "พัน", "หมื่น", "แสน", "ล้าน");
		$zerobahtshow = 0; // ในกรณีที่มีแต่จำนวนสตางค์ เช่น 0.25 หรือ .75 จะให้แสดงคำว่า ศูนย์บาท หรือไม่ 0 = ไม่แสดง, 1 = แสดง
		(string) $number;
		$number = explode(".", $number);
		if(!empty($number[1])){
			if(strlen($number[1]) == 1){
				$number[1] .= "0";
			}else if(strlen($number[1]) > 2){
				if($number[1]{
					2} < 5){
						$number[1] = substr($number[1], 0, 2);
					}else{
						$number[1] = $number[1]{
							0}.($number[1]{
								1}+1);
					}
			}
		}
	
		for($i=0; $i<count($number); $i++){
			$countnum[$i] = strlen($number[$i]);
			if($countnum[$i] <= 7){
				$var[$i][] = $number[$i];
			}else{
				$loopround = ceil($countnum[$i]/6);
				for($j=1; $j<=$loopround; $j++){
					if($j == 1){
						$slen = 0;
						$elen = $countnum[$i]-(($loopround-1)*6);
					}else{
						$slen = $countnum[$i]-((($loopround+1)-$j)*6);
						$elen = 6;
					}
					$var[$i][] = substr($number[$i], $slen, $elen);
				}
			}
	
			$nstring[$i] = "";
			for($k=0; $k<count($var[$i]); $k++){
				if($k > 0) $nstring[$i] .= $t2[7];
				$val = $var[$i][$k];
				$tnstring = "";
				$countval = strlen($val);
				for($l=7; $l>=2; $l--){
					if($countval >= $l){
						$v = substr($val, -$l, 1);
						if($v > 0){
							if($l == 2 && $v == 1){
								$tnstring .= $t2[($l)];
							}elseif($l == 2 && $v == 2){
								$tnstring .= $t2[1].$t2[($l)];
							}else{
								$tnstring .= $t1[$v].$t2[($l)];
							}
						}
					}
				}
				if($countval >= 1){
					$v = substr($val, -1, 1);
					if($v > 0){
						if($v == 1 && $countval > 1 && substr($val, -2, 1) > 0){
							$tnstring .= $t2[0];
						}else{
							$tnstring .= $t1[$v];
						}
	
					}
				}
	
				$nstring[$i] .= $tnstring;
			}
	
		}
		$rstring = "";
		if(!empty($nstring[0]) || $zerobahtshow == 1 || empty($nstring[1])){
			if($nstring[0] == "") $nstring[0] = $t1[0];
			$rstring .= $nstring[0]."บาท";
		}
		if(count($number) == 1 || empty($nstring[1])){
			$rstring .= "ถ้วน";
		}else{
			$rstring .= $nstring[1]."สตางค์";
		}
		return $rstring;
	}
}